"""
自定义分页组件

def pretty_list(request):
    data_dict = {}
    search_data = request.GET.get("q", '')
    if search_data:
        data_dict["mobile__contains"] = search_data

    # 1. 根据自己的情况去筛选自己的数据
    queryset = PrettyNum.objects.all().order_by('-level')
    # 2.实例化分页对象
    page_object = Pagination(request, queryset)


    context = {
        "search_data": search_data,
        "queryset": page_object.page_queryset,          # 分完页的数据
        "page_string": page_object.html(),              # 生成页码
    }

    return render(request, "pretty_list.html", context)


    #######在html页面中######

      <ul class="pagination">
            {{ page_string }}

            <li style="margin-left: 15px">

                <form method="get" style="float: left;margin-left: -1px">
                    <div class="input-group" style="width: 100px">
                        <input type="text" name="page"
                               style="position: relative;float: left;display: inline-block;width:100px;border-radius: 0"
                               class="form-control" placeholder="页码">
                        <span class="input-group-btn">
                    <button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>
                </span>
                    </div>

                </form>
            </li>
        </ul>
"""
from django.utils.safestring import mark_safe
import  copy

class Pagination(object):
    def __init__(self, request, queryset, page_param="page", page_size=10, plus=5):
        self.request = request
        self.queryset = queryset
        self.page_param = page_param
        self.page_size = page_size
        self.plus = plus

        query_dict = copy.deepcopy(self.request.GET)
        query_dict._mutable = True
        self.query_dict = query_dict
        query_dict.setlist('page',[111])
        # 获取当前页码
        self.current_page = int(request.GET.get(page_param, 1))
        self.start = (self.current_page - 1) * page_size
        self.end = self.current_page * page_size

        # 分页数据
        self.page_queryset = queryset[self.start:self.end]

        # 数据总条数
        self.total_count = queryset.count()
        # 总页面数
        self.total_page_count, div = divmod(self.total_count, page_size)
        if div:
            self.total_page_count += 1

    def html(self):
        # 计算显示的页码范围
        if self.total_page_count <= 2 * self.plus + 1:
            start_page = 1
            end_page = self.total_page_count
        elif self.current_page <= self.plus:
            start_page = 1
            end_page = 2 * self.plus + 1
        elif self.current_page + self.plus > self.total_page_count:
            start_page = self.total_page_count - 2 * self.plus
            end_page = self.total_page_count
        else:
            start_page = self.current_page - self.plus
            end_page = self.current_page + self.plus

        # 保留原有查询参数
        params = self.request.GET.copy()
        if self.page_param in params:
            del params[self.page_param]

        # 页码
        page_str_list = []

        # 首页
        params[self.page_param] = 1
        page_str_list.append('<li class="page-item"><a class="page-link" href="?{}">首页</a></li>'.format(params.urlencode()))
        # 上一页
        if self.current_page > 1:
            params[self.page_param] = self.current_page - 1
            prev = '<li class="page-item"><a class="page-link" href="?{}">上一页</a></li>'.format(params.urlencode())
        else:
            params[self.page_param] = 1
            prev = '<li class="page-item"><a class="page-link" href="?{}">上一页</a></li>'.format(params.urlencode())
        page_str_list.append(prev)

        # 中间页码
        for i in range(start_page, end_page + 1):
            params[self.page_param] = i
            if i == self.current_page:
                ele = '<li class="active"><a class="page-link" href="?{}">{}</a></li>'.format(params.urlencode(), i)
            else:
                ele = '<li class="page-item"><a class="page-link" href="?{}">{}</a></li>'.format(params.urlencode(), i)
            page_str_list.append(ele)

        # 下一页
        if self.current_page < self.total_page_count:
            params[self.page_param] = self.current_page + 1
            next_page = '<li class="page-item"><a class="page-link" href="?{}">下一页</a></li>'.format(params.urlencode())
        else:
            params[self.page_param] = self.total_page_count
            next_page = '<li class="page-item"><a class="page-link" href="?{}">下一页</a></li>'.format(params.urlencode())
        page_str_list.append(next_page)

        # 尾页
        params[self.page_param] = self.total_page_count
        page_str_list.append('<li class="page-item"><a class="page-link" href="?{}">尾页</a></li>'.format(params.urlencode()))

        page_string = mark_safe("".join(page_str_list))
        return page_string

